<template>
  <div class="wrapper">
    <el-carousel :interval="4000" type="card" trigger="click" height="250px">
      <el-carousel-item v-for="(item,index) in picture" :key="index" >
        <div class="imgBox" :data-text="item.title" @click="toDetail(item.id)">
          <el-image :src="item.src" fit="fill"></el-image>
        </div>
      </el-carousel-item>
    </el-carousel>
    <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="1">推荐</el-menu-item>
      <el-menu-item index="2">旅游</el-menu-item>
      <el-menu-item index="3">美食</el-menu-item>
      <el-menu-item index="4">礼仪</el-menu-item>
      <el-menu-item index="5">购物</el-menu-item>
    </el-menu>
    <div class="line"></div>
    <div class="news">
      <div v-for="(item,i) in newsData" :key="i" @click="toDetail(item.id)">
       <el-card shadow="hover">
        <div class="items">
          <div class="item title">{{item.title}}</div>
          <div class="item like"><i class="el-icon-star-off"></i><span>{{item.like}}</span></div>
          <div class="item date">{{item.time}}</div>
        </div>
        </el-card>
      </div>
    </div>
    <div class="block" v-show="noRecommend" >
      <el-pagination
        layout="prev, pager, next"
        :total="pages"
        :current-page.sync="currentPage"
        @current-change="changePage">
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      activeIndex: '1',
      pages: 10,
      currentPage: 1,
      noRecommend: false,
      port: this.ports.news.recommend,
      newsData: '',
      picture: [
        {
          src: '//hbimg.huabanimg.com/3847a936093c9d6f3bbde611f86e0d795feb3fb0642d9-wU2vpP_fw658',
          title: '今夏在日本成为最大赢家的薄荷巧克力还有如此玄机！',
          id: '00000000029'
        },
        {
          src: '//hbimg.huabanimg.com/0c0433b9c45cf166322d1da9ca28e3b9c0138ab02078c-M4xReA_fw658',
          title: '富士山不为人知的三大魅力',
          id: '00000000035'
        },
        {
          src: '//hbimg.huabanimg.com/e0dc47b46d82d269a1482ed4be9c6a941d321dbd11a7a-esrkps_fw658',
          title: '被误解的日本文化：饭前双手合十？',
          id: '00000000003'
        },
        {
          src: '//hbimg.huabanimg.com/92267541761c50e9a000fe2ce0b34a70b717d42650339-G9HV21_fw658',
          title: '【日范流行元素】六款流行包包大盘点',
          id: '00000000068'
        },
        {
          src: '//hbimg.huabanimg.com/604c6093f8f58971421462ba958cbbaae40486f2184c1-1U7ehk_fw658',
          title: '夏天日式早餐好选择：手残党也可以做的金枪鱼奶酪吐司',
          id: '00000000010'
        },
        {
          src: '//hbimg.huabanimg.com/417adf3f2157dc09851c2545fd94acc1ff5f5dd67367d-cZCBwS_fw658',
          title: '桜の通り抜け：陪你走过那一段樱花小径',
          id: '00000000033'
        }
      ]
    }
  },
  methods: {
    handleSelect (key) {
      let page = 1
      if (key === '1') {
        this.port = this.ports.news.recommend
        this.noRecommend = false
        page = 0
      } else if (key === '2') {
        this.port = this.ports.news.classify + '?type=1&'
        this.noRecommend = true
      } else if (key === '3') {
        this.port = this.ports.news.classify + '?type=2&'
        this.noRecommend = true
      } else if (key === '4') {
        this.port = this.ports.news.classify + '?type=3&'
        this.noRecommend = true
      } else if (key === '5') {
        this.port = this.ports.news.classify + '?type=4&'
        this.noRecommend = true
      }
      this.getData(this.port, page)
      this.currentPage = 1
    },
    toDetail (id) {
      let routeUrl = this.$router.resolve({
        path: '/newsDetail',
        query: {id: id}
      })
      window.open(routeUrl.href, '_blank')
    },
    changePage () {
      this.getData(this.port, this.currentPage)
    },
    getData (port, page) {
      let api = port + 'page=' + page
      if (page === 0) {
        api = port
      }
      this.http.get(api,
        res => {
          if (res.status === 200) {
            let data = res.data.result
            if (data.code === 200) {
              this.newsData = data.msg
              this.pages = data.pages * 10
              if (this.newsData.length === 0) {
                this.getData(this.port, data.pages)
              }
            } else if (data.code === 1 || data.code === -1) {

            }
          } else {
            console.log(res)
          }
        })
    }
  },
  mounted: function () {
    this.getData(this.port, 0)
  }
}
</script>

<style lang='scss' scoped>
  .wrapper {
    width: 70%;
    margin :0.5rem auto;
    padding-top: 50px;
  }
  .el-menu--horizontal>.el-menu-item.is-active {
    border-bottom: 2px solid rgba(240,20,20,.8);
  }
  .news {
    margin-bottom: 1rem;
  }
  .items {
    display: flex;
    justify-content: flex-end;
    flex-flow: row nowrap;
    height: 25px;
    line-height: 25px;
    color: #585a60;
    font-size: 12px;
  }
  .item {
    flex-shrink: 0;
    flex-grow: 0;
  }
  .title {
    flex-grow: 1;
    font-size: 14px;
  }
  .like {
    width: 10%;
    font-size: 12px;
  }
  .el-card {
    width: 70%;
    margin: 0.5rem auto 0 auto;
    padding: 0 0.5rem;
    cursor: pointer;
  }
  .el-card :hover {
    color: #909399;
    line-height: 28px;
  }
  .block{
    padding: 0;
    margin: 50px auto 100px auto;
    width: 50%;
    text-align: center;
  }
  .el-menu{
    margin-top: 50px;
  }
  .el-carousel{
    width: 1000px;
    margin: auto;
  }
  .wrapper /deep/ .el-carousel__container {
    margin-bottom: 20px;
  }
  .imgBox{
    position:relative;
    /*overflow: hidden;*/
  }
  .imgBox:after{
    position:absolute;
    left: 0;
    top: 0;
    display: block;
    width:100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    content: attr(data-text);
    transition:all 0.5s ease;
    transform: translateY(62%);
    color: #FFF;
    text-align: center;
    line-height: 45px;
  }
  .imgBox:hover:after{
    transform: translateY(60%);
  }
</style>
